import requests
from bs4 import BeautifulSoup
import markdownify
import os
from llm import create_post

# 目标URL
urls = [
    "https://www.aihub.cn/tools/ai-image/facechain-fact/",
]
tag_class = "entry-content"

# entry-content
# 发起请求


# 找到所有class为media-content的a标签

# 请求每个链接
print("正在处理的链接: 1")
response = requests.get(urls[0])
sub_soup = BeautifulSoup(response.content, 'html.parser')

panel_body = sub_soup.find('div', class_=tag_class)
if panel_body:
    for post_note in panel_body.find_all('div', class_='post-note'):
        post_note.decompose()
if panel_body:
    for a_tag in panel_body.find_all('a'):
        href = a_tag.get('href', '')
        if 'aihub.cn' in href:
            # 替换链接中的aihub.cn为xxxx.com
            new_href = href.replace('aihub.cn', 'xxxx.com')
            a_tag['href'] = new_href
    # 处理图片和视频等资源
    for media_tag in panel_body.find_all(['img', 'video', 'audio']):
        # 创建一个占位div标签
        placeholder_div = sub_soup.new_tag('div')
        # 保留原始资源链接并替换src中的aihub.cn为xxxx.com
        src = media_tag.get('src', '').replace('aihub.cn', 'xxxx.com')
        placeholder_div.string = f'此处资源已替换: {src}'
        media_tag.replace_with(placeholder_div)
# 找到class是entry-header 的h1标签
header = sub_soup.find('header', class_='entry-header')
if header:
    title = header.find('h1').contents[0]


print(f"{panel_body}title {title}")

if panel_body:
    # 将非法字符替换为下划线
    safe_title = "".join([c if c.isalnum() else "-" for c in title])
    # 转换为markdown
    markdown_content = markdownify.markdownify(str(panel_body))
    result = create_post(markdown_content)
    # 创建以标题命名的子目录
    os.makedirs(f'markdown_files/{safe_title}', exist_ok=True)
    # 保存到文件
    file_path = f'markdown_files/{safe_title}/{safe_title}.md'
    with open(file_path, 'w', encoding='utf-8') as f:
        f.write(f"# {title}\n\n")
        f.write(result)
    print(f"已保存: {safe_title}.md")
